<?php
require './model/Db.php';
$Db = Db::getInstance();
$user_data = $Db->islogin();
$isExist = $Db->find([
	table => ['zone'],
	where => "id = {$_GET['zid']}"
]);
$isMobile = preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT']);
$target = $isMobile?'':'target="_blank"';

if($isExist){
	//收藏
	$collect_mids = json_decode($user_data['my_follow'])->mids;
	//版面分组
	$kind = $Db->find([
		table => ['kind'],
		field => 'id,name',
		where => "zid = 0 or zid = {$_GET['zid']} order by sort,id"
	]);
	$module_data = $Db->find([
		table => [module => 'a',kind => 'b'],
		field => 'a.id,a.name,a.info,a.manager,a.kid',
		where => "a.kid = b.id and a.zid = {$_GET['zid']}"
	]);
	$data_arr = [];
	foreach ($module_data as $item) {
		if($user_data){
			$item['collect'] = in_array($item['id'], $collect_mids)?' collected':'';
		}
		
		//版务
		$manager = json_decode($item['manager'])->uids;
		$uids = implode(',', $manager);
		if(!$uids){
			$uids = -1;
		}
		$manager_info = $Db->find([
			table => ['bbs_user'],
			field => 'id,user',
			where => "id in ({$uids})"
		]);
		$item['manager'] = $manager_info;
		//今日
		$item['today'] = $Db->count([
			table => [content => 'a',module => 'b'],
			where => "a.mid = b.id and b.id = {$item['id']} and a.time > CURDATE()"
		]);
		//主题
		$item['main'] = $Db->count([
			table => [content => 'a',module => 'b'],
			where => "a.mid = b.id and b.id = {$item['id']} and a.cid = 0"
		]);
		//总数
		$item['all'] = $Db->count([
			table => [content => 'a',module => 'b'],
			field => 'a.title,a.time,a.uid,a.id,a.cid',
			where => "a.mid = b.id and b.id = {$item['id']} order by a.time desc"
		]);
		$all_cont = $Db->find([
			table => [content => 'a',module => 'b'],
			field => 'a.title,a.time,a.uid,a.id,a.cid',
			where => "a.mid = b.id and b.id = {$item['id']} and a.delete_time is NULL order by a.time desc limit 0,1"
		])[0];
		if($all_cont){
			$user_info = $Db->find([
				table => ['bbs_user'],
				field => 'id,photo,user',
				where => "id = {$all_cont['uid']}"
			])[0];
			$all_cont['user_info'] = $user_info;
		}
		$item['content'] = $all_cont;
		$data_arr[] = $item;
	}
	//排序数组
	function my_sort($a,$b)
	{
		if ($a['all']==$b['all']) return 0;
		return ($a['all']>$b['all'])?-1:1;
	}
	usort($data_arr,"my_sort");
}else{
	header('Location:err.php?id=0');
}

$Db->close();
$web_title = $isExist[0]['name'];
$css = [
	'public/css/my-bootstrap.min.css',
	'public/css/list.all.min.css'
];
include './view/header.html';
include './view/list.all.html';
include './view/footer.html';